import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:有效三角形个数
 * User: bjc
 * Date: 2025-11-05
 * Time: 10:41
 */
public class Test05_triangleNumber {
    public static int triangleNumber(int[] nums) {
        //1.先把数组排序
        Arrays.sort(nums);
        //2.使用双指针解决
        int count = 0;
        for(int i = nums.length-1;i >= 2;i--){//i>=2是因为需要找到三个数，i<2的时候无法找到三个数
            //定义左右指针
            int left = 0,right = i-1;
            while (left < right){
                if(nums[left] + nums[right] > nums[i]){
                    count += right - left;
                    right--;
                }else {
                    left++;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] nums = {4,2,3,4};
        System.out.println(triangleNumber(nums));
    }
}
